<template>
  <!-- 一条文章单元格 -->
  <van-cell>
    <!-- 标题区域的插槽 -->
    <template #title>
      <div class="title-box">
        <!-- 标题 -->
        <span>{{item.title}}</span>
        <!-- 单图 -->
        <img class="thumb" :src="item.cover.images[0]" v-if="item.cover.type == 1" />
      </div>
      <!-- 三张图片 -->
      <div class="thumb-box" v-if="item.cover.type > 1">
        <img class="thumb" :src="pic" v-for="(pic,index) in item.cover.images" :key="index" />
      </div>
    </template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <span>作者: {{item.aut_name}}</span>
          <span>{{item.comm_count}}评论</span>
          <span>{{ $timeAgo(item.pubdate) }}</span>
        </div>
        <!-- 反馈按钮 -->
        <van-icon name="cross" @click.stop="onCloseClick" v-if="isCross" />
        <!-- 反馈面板 -->
        <van-action-sheet v-model="show" :actions="actions" :cancel-text="cancelText"
          get-container="body" @select="onSelect" @cancel="onCancel" />
        <!-- 其它问题的弹出层 -->
        <van-dialog v-model="showDialog" title="请输入反馈的内容" show-cancel-button get-container="body"
          :beforeClose="onBeforeCloseFn">
          <van-field type="textarea" placeholder="请输入反馈的内容" v-model.trim="msg" v-myfocus />
        </van-dialog>
      </div>
    </template>
  </van-cell>
</template>

<script>
import { firstActions, secondActions } from '@/api/report'
import { articleDislikeAPI, articleReportsAPI } from '@/api/home'
export default {
  name: 'ArticleItem',
  data () {
    return {
      show: false,
      showDialog: false,
      actions: firstActions,
      cancelText: '取消',
      msg: '' // 其他问题的内容
    }
  },
  props: {
    item: {
      type: Object,
      required: true
    },
    i: {
      type: Number
    },
    isCross: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    // 点击x号出现反馈面板
    onCloseClick () {
      this.show = true
    },
    // 反馈面板点击事件
    async onSelect (action) {
      // 不感兴趣功能
      if (action.name === '不感兴趣') {
        try {
          // 1. 调接口, 让后台删除这篇文章
          await articleDislikeAPI({
            artId: this.item.art_id
          })
          this.$toast.success('操作成功!')
          // 2. 从vue变量中删除当前文章对象 (数据驱动视图)
          this.$emit('dislike', this.i)
          this.show = false // 关闭弹窗
        } catch (e) {
          this.$toast.success('操作失败!')
        }
        this.show = false
      } else if (action.name === '反馈垃圾内容') { // 显示二级面板
        this.actions = secondActions
        this.cancelText = '返回' // 修改底部按钮显示文字
      } else if (action.name === '拉黑作者') { // 拉黑作者
        console.log('未完成 拉黑作者')
      } else { // 二级菜单内的功能
        // 二级菜单内的功能
        if (action.name === '其它问题') {
          this.showDialog = true
        } else {
          try {
            await articleReportsAPI({
              target: this.item.art_id,
              type: action.id + '',
              remark: action.name
            })
            this.$toast.success('反馈成功')
          } catch (e) {
            this.$toast.fail('反馈失败')
          }
        }

        setTimeout(() => {
          this.actions = firstActions
        }, 1000)
        this.show = false
      }
    },
    // 弹出层关闭前的回调
    async onBeforeCloseFn (action, done) {
      if (action === 'cancel') {
        return done()
      }
      if (!this.msg) {
        this.$toast.fail('请输入反馈内容')
        done(false)
        return
      }
      try {
        await articleReportsAPI({
          target: this.item.art_id,
          type: '0',
          remark: this.msg
        })
        this.$toast.success('反馈成功')
        this.msg = ''
      } catch (e) {
        this.$toast.fail('反馈失败')
      } finally {
        done()
      }
    },
    // 面板取消事件
    onCancel () {
      if (this.cancelText === '返回') {
        this.actions = firstActions
        this.cancelText = '取消'
        this.show = true // 点返回不要让弹窗关闭, 点取消文字按钮, 弹窗默认关闭
      }
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}

/* 图片样式 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
